package com.bhs.sansong.ui.order;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.RefuseAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseBean;
import com.bhs.sansong.bean.TypeBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 取消订单
 */
public class CancelOrderActivity extends BaseActivity implements View.OnClickListener, NetCallBack {


    private RecyclerView mRvList;
    private EditText mEtReason;
    private Button mBtnConfirm;
    private String deliver_sn;
    private List<TypeBean> list = new ArrayList<>();

    @Override
    public int initLayout() {
        return R.layout.activity_cancel_order;
    }

    @Override
    public void initView() {
        deliver_sn = getIntent().getStringExtra("deliver_sn");
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("取消订单");
        mRvList = findViewById(R.id.rv_list);
        mEtReason = findViewById(R.id.et_reason);
        mBtnConfirm = findViewById(R.id.btn_confirm);
        mBtnConfirm.setOnClickListener(this);
    }

    @Override
    public void initData() {
        list.add(new TypeBean("长时间未接单" ));
        list.add(new TypeBean("无法将货物送往专线"));
        list.add(new TypeBean("不想发货了"));
        list.add(new TypeBean("费用过高"));
        list.add(new TypeBean("货物数量错误"));
        list.add(new TypeBean("地址信息错误"));
        list.add(new TypeBean("其他"));
        RefuseAdapter adapter = new RefuseAdapter(R.layout.item_refuse, list);
        mRvList.setAdapter(adapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (TypeBean typeBean : list) {
                    typeBean.setChecked(false);
                }
                list.get(position).setChecked(true);
                adapter.notifyDataSetChanged();
                if (list.get(position).getTypeName().equals("其他")) {
                    mEtReason.setVisibility(View.VISIBLE);
                } else {
                    mEtReason.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {

        String refuse = null;
        for (TypeBean typeBean : list) {
            if (typeBean.isChecked()) {
                refuse = typeBean.getTypeName();
            }
        }
        if (mEtReason.getVisibility() == View.VISIBLE) {
            if (TextUtils.isEmpty(mEtReason.getText())) {
                ToastUtil.show("请输入其他原因");
                return;
            }
            refuse = mEtReason.getText().toString();
        }
        networkRequest(netApi.cancel_order(deliver_sn, refuse), CancelOrderActivity.this);

    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean bean = new Gson().fromJson(json, BaseBean.class);
        ToastUtil.show(bean.getMsg());
        if (bean.isStatus()) {
            finish();
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}